package com.example.widget_lib.widget;

import com.example.widget_lib.utils.PxUtil;
import com.example.widget_lib.utils.Util;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import ohos.media.image.PixelMap;

public class ImgTxtMixtureView extends Component implements Component.DrawTask {
    public static final int IMAGE_PADDING =  (int) PxUtil.vp2px(62);
    private static final int IMAGE_WIDTH = (int) PxUtil.vp2px(144);
    Paint mTextPaint = new Paint();
    Paint mPaint = new Paint();
    Paint.FontMetrics mMetrics;
    int mScreenW;
    int mScreenH;
    private String text = "Save the date! Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.The App Bundle speaks Duolingos language, reducing its app size by 56%\n" +
            "Since 2011, Duolingo has made language learning fun for millions of people worldwide. Offering free King is a leading interactive entertainment company, with popular mobile games " +
            "such as Candy Crush Saga, Farm Heroes Saga and Bubble Witch 3 Saga. In March 2018, King implemented Google Play Instant and was excited to see the potential impact on removing user " +
            "acquisition friction, targeting audiences more efficiently, and increasing the effectiveness of game cross-promotion.Saga and Bubble Witch 3 Saga. In March 2018, King implemented Google Play Instant and was excited to see the potential impact on removing user" +
            "acquisition friction, targeting audiences more efficiently, and increasing the effectiveness of game cross-promotion";

    private PixelMap mBitmap;

    {
        try {
            mTextPaint.setTextSize((int) PxUtil.fp2px(15));
            mBitmap = Util.getAvatar(this, IMAGE_WIDTH);
            mPaint.setFont(Font.DEFAULT);
            mPaint.setTextSize((int) PxUtil.fp2px(16));
            mPaint.setColor(Color.BLACK);
            mMetrics = mPaint.getFontMetrics();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ImgTxtMixtureView(Context context) {
        super(context);
        addDrawTask(this);
    }

    public ImgTxtMixtureView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        int[] screenWH = Util.getScreenWH(context);
        mScreenW = screenWH[0];
        mScreenH = screenWH[1];
        addDrawTask(this);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        int length = text.length();
        float yOffset = mPaint.getFontMetrics().bottom - mPaint.getFontMetrics().top;
        int usableWidth;
        float wid = mPaint.measureText(text.toLowerCase().substring(0, 1));

        canvas.drawPixelMapHolder(new PixelMapHolder(mBitmap), mScreenW - IMAGE_WIDTH, IMAGE_PADDING, mPaint);
        for (int start = 0, count; start < length; start += count, yOffset += (mPaint.getFontMetrics().bottom - mPaint.getFontMetrics().top)) {
            float textTop = yOffset + mPaint.getFontMetrics().top;
            float textBottom = yOffset + mPaint.getFontMetrics().bottom;
            if (textTop > IMAGE_PADDING && textTop < IMAGE_PADDING + IMAGE_WIDTH ||
                    textBottom > IMAGE_PADDING && textBottom < IMAGE_PADDING + IMAGE_WIDTH) {
                usableWidth = mScreenW - IMAGE_WIDTH;
            } else {
                usableWidth = mScreenW;
            }
            count = (int) (usableWidth / wid);
            if (start + count >= length) {
                count = length - start;
            }
            String temp = text.substring(start, start + count);
            canvas.drawText(mPaint, temp, 0, yOffset);
        }
    }
}
